package Server.controller;

import Plug.operLog.OperLog;
import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.DictionarysDao;
import Server.entity.Diction;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("SwiperController")
public class SwiperController {
    @Autowired
    private DictionarysDao dictionarysDao;
    @RequestMapping("List")
    public RetResult List(){
        return RetResponse.makeOKRsp(dictionarysDao.findByTypeOrderByKeystAsc("SWIPER"));
    }
    @RequestMapping("PayList")
    public RetResult PayList(){
        return RetResponse.makeOKRsp(dictionarysDao.findByTypeOrderByKeystAsc("PAY"));
    }
    @RequestMapping("MailList")
    public RetResult MailList(){
        return RetResponse.makeOKRsp(dictionarysDao.findByTypeOrderByKeystAsc("MAIL"));
    }
    @OperLog("邮寄方式保存")
    @RequestMapping("SaveMail")
    public RetResult SaveMail(@RequestBody Diction diction){
        try {
            if(StringUtils.isEmpty(diction.getUuid())){
                diction.setType("MAIL");
                dictionarysDao.save(diction);
            }else{
                Diction entity = dictionarysDao.findByUuid(diction.getUuid());
                entity.setVal(diction.getVal());
                entity.setKeyst(diction.getKeyst());
                dictionarysDao.save(entity);
            }
        }catch (Exception e){
            return RetResponse.makeErrRsp("排序已存在！");
        }
        return RetResponse.makeOKRsp();
    }

    @OperLog("轮播保存")
    @RequestMapping("Save")
    public RetResult Save(@RequestBody Diction diction){
        try {
            if(StringUtils.isEmpty(diction.getUuid())){
                diction.setType("SWIPER");
                dictionarysDao.save(diction);
            }else{
                Diction entity = dictionarysDao.findByUuid(diction.getUuid());
                entity.setVal(diction.getVal());
                dictionarysDao.save(entity);
            }
        }catch (Exception e){
            return RetResponse.makeErrRsp("排序已存在！");
        }
        return RetResponse.makeOKRsp();
    }
    @OperLog("轮播删除")
    @RequestMapping("Deleted")
    public RetResult Deleted(@RequestBody Diction diction){
        dictionarysDao.delete(dictionarysDao.findByUuid(diction.getUuid()));
        return RetResponse.makeOKRsp();
    }
}
